package com.zhongxi.guoyuan.OffLineShop.Adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.OffLineShop.Model.OffLineHasConsumeResult;
import com.zhongxi.guoyuan.OffLineShop.UI.OffLineShopDetailActivtiy;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.CommonTool;
import com.zhongxi.guoyuan.utils.DataUtil;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/1/24.
 */

public class OfflineHasConsumeAdapter extends BaseAdapter {

    private Context context;
    private List<OffLineHasConsumeResult.OrderInfoBean> list;
    private JSONObject shopInfo;
    private Handler handler;
    private OffLineHasConsumeResult.OrderInfoBean infoBean;

    public OfflineHasConsumeAdapter(Context context, Handler handler) {
        this.context = context;
        this.handler=handler;
        list = new ArrayList<>();
    }

    public void setList(List<OffLineHasConsumeResult.OrderInfoBean> list,JSONObject shopInfo) {
        this.list = list;
        this.shopInfo=shopInfo;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView==null){
            convertView=LayoutInflater.from(context).inflate(R.layout.offline_hasconsume_layout, null);
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
         holder= (ViewHolder) convertView.getTag();
        }

        if(null!=list&&!"".equals(list)){
            if(null!=list.get(position)&&!"".equals(list.get(position))){
                infoBean = list.get(position);
                Glide.with(context).load(Url.IMG_URL+infoBean.getShopimage()).placeholder(R.mipmap.hicc).into(holder.offlineHasconsumeImage);
                
                if(null!=infoBean.getPayprice()&&!"".equals(infoBean.getPayprice())){
                    holder.offlineHasconsumeRealprice.setText(infoBean.getPayprice()+"元");
                    if (DataUtil.equalZero(infoBean.getPayprice())){
                        holder.offlineHasconsumeRealprice.setText("");
                    }
                }else{
                    holder.offlineHasconsumeRealprice.setText("");
                }
                if(null!=infoBean.getPayredbean()&&!"".equals(infoBean.getPayredbean())){
                    holder.offlineHasconsumeRealredbean.setText(" + "+infoBean.getPayredbean()+"红豆");
                    if (DataUtil.equalZero(infoBean.getPayprice())){
                        holder.offlineHasconsumeRealredbean.setText(infoBean.getPayredbean()+"红豆");
                    }
                }else{
                    holder.offlineHasconsumeRealredbean.setText("");
                }
                if(null!=infoBean.getBackinnerprice()&&!"".equals(infoBean.getBackinnerprice())){
                    holder.offlineHasconsumeBackinnerbean.setText(infoBean.getBackinnerprice()+"美豆");
                }else{
                    holder.offlineHasconsumeBackinnerbean.setText("");
                }
                if(null!=infoBean.getCtime()&&!"".equals(infoBean.getCtime())){
                    holder.offlineHasconsumeTime.setText("时间："+infoBean.getCtime());
                }else{
                    holder.offlineHasconsumeTime.setText("时间：");
                }
                if(null!=shopInfo&&!"".equals(shopInfo)){
                    if(null!=infoBean.getShopid()&&!"".equals(infoBean.getShopid())){

                        JSONObject shopobj=shopInfo.optJSONObject(infoBean.getShopid());
                        if(null!=shopobj&&!"".equals(shopobj)){
                            holder.offlineHasconsumeShopname.setText(shopobj.optString("name"));
                            holder.offlineHasconsumeAddress.setText("地址："+shopobj.optString("address"));
                        }else{
                            holder.offlineHasconsumeShopname.setText("");
                            holder.offlineHasconsumeAddress.setText("地址："+shopobj.optString("address"));
                        }
                    }

                }
                holder.offlineHasconsumeDelete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AlertDialog dialog=new AlertDialog.Builder(context).setTitle("您确定要删除该项目吗？")
                                .setPositiveButton("删除", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        Message msg = Message.obtain();
                                        msg.obj = position;
                                        msg.what = 0;
                                        handler.sendMessage(msg);
                                        dialog.dismiss();
                                    }
                                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                }).show();
                        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(context.getResources().getColor(R.color.bg_yield));
                        dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(context.getResources().getColor(R.color.bg_yield));
                    }
                });
                convertView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(context, OffLineShopDetailActivtiy.class);
                        intent.putExtra("offlineshopid",list.get(position).getShopid());
                        context.startActivity(intent);
                    }
                });
            }
        }

        return convertView;
    }

    static class ViewHolder {
        @Bind(R.id.offline_hasconsume_image)
        ImageView offlineHasconsumeImage;
        @Bind(R.id.offline_hasconsume_shopname)
        TextView offlineHasconsumeShopname;
        @Bind(R.id.offline_hasconsume_realprice)
        TextView offlineHasconsumeRealprice;
        @Bind(R.id.offline_hasconsume_realredbean)
        TextView offlineHasconsumeRealredbean;
        @Bind(R.id.offline_hasconsume_backinnerbean)
        TextView offlineHasconsumeBackinnerbean;
        @Bind(R.id.offline_hasconsume_time)
        TextView offlineHasconsumeTime;
        @Bind(R.id.offline_hasconsume_delete)
        TextView offlineHasconsumeDelete;
        @Bind(R.id.offline_hasconsume_address)
        TextView offlineHasconsumeAddress;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
